package com.example.windowdemo.window;

import android.content.Context;
import android.view.View;

import com.example.windowdemo.callback.WindowCommand;
import com.example.windowdemo.window.command.SetDataCommand;
import com.example.windowdemo.window.command.ShowWindowCommand;

import java.util.Arrays;
import java.util.List;

/**
 * @author: njb
 * @date: 2023/8/3 11:40
 * @desc:
 */
public class WindowShowManager<T> {
    private Context context;
    private T data;

    public WindowShowManager(Context context) {
        this.context = context;
    }

    public CommandWrapper createWindow(View view, T data) {
        this.data = data;
        Window<T> window = new Window<>(context);
        window.addView(view);

        WindowCommand showCommand = new ShowWindowCommand(window);
        WindowCommand setDataCommand = new SetDataCommand<>(window, data);

        return new CommandWrapper(showCommand, setDataCommand);
    }

    public void execute(CommandWrapper commandWrapper) {
        commandWrapper.execute();
    }

    public void cancel(CommandWrapper commandWrapper) {
        commandWrapper.cancel();
    }

    public T getData(){
        return data;
    }

    public static class CommandWrapper {
        private List<WindowCommand> commands;

        public CommandWrapper(WindowCommand... commands) {
            this.commands = Arrays.asList(commands);
        }

        public void execute() {
            for (WindowCommand command : commands) {
                command.execute();
            }
        }

        public void cancel() {
            for (int i = commands.size() - 1; i >= 0; i--) {
                commands.get(i).cancel();
            }
        }



    }

}
